//
//  PicCollectionView.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/22.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit
import SDWebImage

class PicCollectionView: UICollectionView {
    var picURLs : [NSURL] = [NSURL](){
        didSet{
            //计算图片高度
            let (width, height) = calculatePicCollectionSize(picURLs.count)
            picCollectionViewWidthCons.constant = width
            picCollectionViewHeightCons.constant = height
       //刷新
        reloadData()
        }
    }
    //图片宽度约束
    
    @IBOutlet weak var picCollectionViewWidthCons: NSLayoutConstraint!
    //图片高度约束
    
    @IBOutlet weak var picCollectionViewHeightCons: NSLayoutConstraint!
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 1.设置数据源
        dataSource = self
//        // 2.设置布局
//        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
//        //计算图片的宽度和高度
//        let imageWH = (UIScreen.mainScreen().bounds.width - 2 * margin - 2 * padding) / 3
//        layout.itemSize = CGSize(width: imageWH, height: imageWH)
//        layout.minimumInteritemSpacing = padding
//        layout.minimumLineSpacing = padding
    }
}
extension PicCollectionView{
    private func calculatePicCollectionSize(count : Int) -> (CGFloat, CGFloat){
        //计算行数
        let row = CGFloat((count - 1) / 3 + 1)
       
        //没有图片
        if count == 0{
            return (0,0)
        }
        //设置布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout

        //一张图片
        if count == 1{
            let urlString = picURLs.last!.absoluteString
            //imageFromDiskCacheForKey--通过某个key从磁盘中取出对应的图片imageFromMemoryCacheForKey--通过某个key从内存中取出对应的图片
       let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlString)
            //设置流水布局
            let width = image.size.width * 2
            let height = image.size.height * 2
            layout.itemSize = CGSize(width: width, height: height)
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
            
            return (width, height)
        }
        //计算图片的宽度和高度
        let imageWH = (UIScreen.mainScreen().bounds.width - 2 * margin - 2 * padding) / 3
        layout.itemSize = CGSize(width: imageWH, height: imageWH)
        layout.minimumInteritemSpacing = padding
        layout.minimumLineSpacing = padding
        //四张图片
        if count == 4{
            let width = 2 * imageWH + padding + 1
            let height = 2 * imageWH + padding
            return (width,height)
        }
        //其他
        let width = 3 * imageWH + 2 * padding
        let height = row * imageWH  + (row - 1) * padding
        
        return (width, height)
    }
    

}
extension PicCollectionView : UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picURLs.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("picCollectionViewID", forIndexPath: indexPath) as! picCollectionViewCell
//        cell.backgroundColor = UIColor.greenColor()
        cell.picURL = picURLs[indexPath.row]
        return cell
    }
}
class picCollectionViewCell : UICollectionViewCell {
    var picURL : NSURL?{
        didSet{
        picImageView.sd_setImageWithURL(picURL, placeholderImage: UIImage(named: "empty_picture"))
        }
    }
    @IBOutlet weak var picImageView: UIImageView!
    
}